package sing.cloud.alibaba.nacos.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author xing
 * @date 2022/8/24 17:03
 */
@SpringBootApplication
@Slf4j
public class NacosConsumerApplication {

    @RestController
    public class NacosController{

        @Autowired
        private RestTemplate restTemplate;

        @GetMapping("/echo/app-name")
        public String echoAppName(){
            return restTemplate.getForObject("http://cloud-nacos-provider/echo/cloud-nacos-consumer", String.class);
        }

    }

    //实例化 RestTemplate 实例
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){

        return new RestTemplate();
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(NacosConsumerApplication.class, args);
        String[] beanNamesForType = run.getBeanNamesForType(LoadBalancerClient.class);
            log.warn("-===============");
        for (String s : beanNamesForType) {
            log.warn(s);
        }
        log.warn("====================");
    }
}
